package com.example.demo.controller;

import com.example.demo.annotation.Api;
import com.example.demo.domain.PageResult;
import com.example.demo.domain.entity.VojSubmissions;
import com.example.demo.domain.exam.Exam;
import com.example.demo.domain.exam.ExamPaper;
import com.example.demo.domain.exam.ExamSubmitLog;
import com.example.demo.domain.exam.ExamUserAbnormalBehavior;
import com.example.demo.domain.exam.FilePath;
import com.example.demo.domain.exam.QueryModel;
import com.example.demo.domain.exam.UserExamAns;
import com.example.demo.domain.exam.condition.ExamCondition;
import com.example.demo.dto.LatestSubmissionResultDto;
import com.example.demo.dto.UserExamAnswerDTO;
import com.example.demo.service.ExamForUserService;
import com.example.demo.service.VojSubmissionsService;
import com.example.demo.service.exam.ExamSubmitLogService;
import com.example.demo.service.exam.ExamUserAbnormalBehaviorService;
import com.example.demo.service.exam.FilePathService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/exam/user")
public class ExamForUserController {

    private final ExamForUserService examForUserService;

    private final FilePathService filePathService;

    private final ExamUserAbnormalBehaviorService examUserAbnormalBehaviorService;

    private final ExamSubmitLogService examSubmitLogService;

    private final VojSubmissionsService vojSubmissionsService;

    @Api   //获取考生的所有考试
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public PageResult<Exam> page(@RequestBody QueryModel<ExamCondition> model) {
        return examForUserService.page(model);
    }

    @Api
    @RequestMapping(value = "/enter", method = RequestMethod.GET)
    public ExamPaper getExamDetail(@RequestParam Long examId, HttpServletRequest request) {
        return examForUserService.getExamDetail(examId, request);
    }

    @Api
    @RequestMapping(value = "/staging", method = RequestMethod.POST)
    public void stagingExamAns(@RequestBody UserExamAnswerDTO dto) {
        examForUserService.stagingExamAns(dto);
    }

    @Api
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public void submitExamAns(@RequestBody UserExamAnswerDTO dto) {
        examForUserService.submitExamAns(dto);
    }

    @Api
    @RequestMapping(value = "/get/ans", method = RequestMethod.GET)
    public List<UserExamAns> getExamAns(@RequestParam Long examId) {
        return examForUserService.getExamAns(examId);
    }

    @Api
    @RequestMapping(value = "/get/staging-ans", method = RequestMethod.GET)
    public List<UserExamAns> getStagingExamAns(Long examId) {
        return examForUserService.getStagingExamAns(examId);
    }

    @Api
    @RequestMapping(value = "/submit-log", method = RequestMethod.POST)
    public void submitExamLog(@RequestBody ExamSubmitLog entity) {
        examForUserService.submitExamLog(entity);
    }

    /**
     * 截图信息上传
     * @param entity 文件实体
     */
    @Api
    @RequestMapping(value = "/screenshot/upload", method = RequestMethod.POST)
    public void screenshotUpload(@RequestBody FilePath entity) {
        filePathService.insert(entity);
    }

    /**
     * 提交用户行为
     * @param entity 行为参数
     */
    @Api
    @RequestMapping(value = "/behavior", method = RequestMethod.POST)
    public void submitUserBehavior(@RequestBody ExamUserAbnormalBehavior entity) {
        examUserAbnormalBehaviorService.insert(entity);
    }

    /**
     * 提交代码评测
     * @param entity 提交记录实体
     * @return 返回submissionId
     */
    @Api
    @RequestMapping(value = "/submit-evaluation", method = RequestMethod.POST)
    public Long submitEvaluation(@RequestBody ExamSubmitLog entity) {
        return examForUserService.submitEvaluation(entity);
    }

    /**
     * 获取代码评测记录
     * @param submissionId 评测机标识
     * @return 提交记录
     */
    @Api
    @RequestMapping(value = "/submit-log/get", method = RequestMethod.POST)
    public LatestSubmissionResultDto getSubmitLog(@RequestParam Long submissionId) {
        return vojSubmissionsService.getSubmitLogBySubmissionId(submissionId);
    }

    /**
     * 获取某个学生某场考试的所有提交记录
     * @param
     * @param examId 考试ID
     * @param
     * @return
     */
    @Api
    @RequestMapping(value = "/question/submit-log/get", method = RequestMethod.GET)
    public List<LatestSubmissionResultDto> getQuestionSubmitLog(@RequestParam Long questionId,@RequestParam Long examId) {
        return vojSubmissionsService.getQuestionSubmitLog(questionId,examId);
    }

}
